﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Rej;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Rej
{
    public class rej_tblContactInfoDAL:Irej_tblContactInfo
    {
        HKEA_CMSEntities model;
        rej_tblContactInfo info;

        public rej_tblContactInfoDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static rej_tblContactInfoDAL GetInstance()
        {
            return new rej_tblContactInfoDAL();
        }

        public int InsertENT(rej_tblContactInfo ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.rej_tblContactInfo.AddObject(ent);
            return model.SaveChanges();
        }

        public List<rej_tblContactInfo> GetListByENT(rej_tblContactInfo ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.rej_tblContactInfo.Where(a => a.ContactInfoUID > 0);
            if (ent.ContactInfoUID > 0)
                result = result.Where(a => a.ContactInfoUID == ent.ContactInfoUID);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<rej_tblContactInfo> GetListByENT(rej_tblContactInfo ent)
        {
            var result = model.rej_tblContactInfo.Where(a => a.ContactInfoUID > 0);
            if (ent.ContactInfoUID > 0)
                result = result.Where(a => a.ContactInfoUID == ent.ContactInfoUID);
            return result.ToList();
        }
        public int GetCountByENT(rej_tblContactInfo ent)
        {
            var result = model.rej_tblContactInfo.Where(a => a.ContactInfoUID > 0);
            if (ent.ContactInfoUID > 0)
                result = result.Where(a => a.ContactInfoUID == ent.ContactInfoUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }
        public int UpdateENT(rej_tblContactInfo ent)
        {
            info = model.rej_tblContactInfo.Where(a => a.ContactInfoUID == ent.ContactInfoUID).FirstOrDefault();
            if (info != null)
            {
                info.CountryCode = ent.CountryCode;
                info.Email1 = ent.Email1;
                info.Email2 = ent.Email2;
                info.Email3 = ent.Email3;
                info.Fax1 = ent.Fax1;
                info.Fax1_AreaCode = ent.Fax1_AreaCode;
                info.Fax1_CountryCode = ent.Fax1_CountryCode;
                info.Fax2 = ent.Fax2;
                info.Fax2_AreaCode = ent.Fax2_AreaCode;
                info.Fax2_CountryCode = ent.Fax2_CountryCode;
                info.Fax3 = ent.Fax3;
                info.Fax3_AreaCode = ent.Fax3_AreaCode;
                info.Fax3_CountryCode = ent.Fax3_CountryCode;
                info.LastModifyBy = ent.LastModifyBy;
                info.LastModifyDate = DateTime.Now;
                info.RejectedBy = ent.RejectedBy;
                info.RejectedDate = ent.RejectedDate;
                info.Tel1 = ent.Tel1;
                info.Tel1_AreaCode = ent.Tel1_AreaCode;
                info.Tel1_CountryCode = ent.Tel1_CountryCode;
                info.Tel2 = ent.Tel2;
                info.Tel2_AreaCode = ent.Tel2_AreaCode;
                info.Tel2_CountryCode = ent.Tel2_CountryCode;
                info.Tel3 = ent.Tel3;
                info.Tel3_AreaCode = ent.Tel3_AreaCode;
                info.Tel3_CountryCode = ent.Tel3_CountryCode;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(rej_tblContactInfo ent)
        {
            var temp = model.rej_tblContactInfo.Where(a => a.ContactInfoUID > 0);
            if (ent.ContactInfoUID > 0)
                temp = temp.Where(a => a.ContactInfoUID == ent.ContactInfoUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.rej_tblContactInfo.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
